﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class TariffDetailBLL : IBaseInfoDetail
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IGeneralLedgerDA generalLedgerDA = DataAccess.CreateGeneralLedgerDA();
        private PubParasInfo pubParasInfo;
        private static readonly IRateDA rateDA = DataAccess.CreateRateDA();
        private static readonly ITariffDetailDA tariffDetailDA = DataAccess.CreateTariffDetailDA();

        public TariffDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public TariffDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public TariffDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> mainID, List<string> TariffType, List<string> pk3)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string temp = string.Empty;
            try
            {
                List<TariffDetailInfo> TariffDetailInfos = new List<TariffDetailInfo>();
                TariffDetailInfo subjectInfo = null;
                foreach (string type in TariffType)
                {
                    subjectInfo = tariffDetailDA.GetTariffDetailByID(dataBase, tran, mainID[0], type, this.dataBaseID[0], this.pubParasInfo);
                    TariffDetailInfos.Add(subjectInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (tariffDetailDA.DeleteTariffDetail(dataBase, tran, mainID[0], TariffType, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < TariffType.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<TariffDetailInfo>(dataBase, tran, TariffDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "TARIFF_DETAIL", this.dataBaseID[0]);
                        temp = temp + TariffType[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllDetailInfo()
        {
            return tariffDetailDA.GetAllTariffDetail(this.dataBaseID[0]);
        }

        public DataSet GetDetaiInfolByQueryEntity(object detailQuery)
        {
            throw new NotImplementedException();
        }

        public object GetDetailInfoByID(string mainID, string subID, string pk3)
        {
            return tariffDetailDA.GetTariffDetailByID(mainID, subID, this.dataBaseID[0], this.pubParasInfo);
        }

        public List<object> GetDetailInfoListByID(string mainID, string subID)
        {
            throw new NotImplementedException();
        }

        public string InsertDetail(object obj)
        {
            TariffDetailInfo tariffDetailInfo = obj as TariffDetailInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strLineID = string.Empty;
            try
            {
                tariffDetailInfo.UpdatedBy = tariffDetailInfo.CreatedBy;
                tariffDetailInfo.CreatedDate = DateTime.Now;
                tariffDetailInfo.UpdatedDate = DateTime.Now;
                if (!generalLedgerDA.CheckGeneralLedgerIDUnique(dataBase, tran, tariffDetailInfo.GeneralLedgerID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F9140_002", this.pubParasInfo.UserLanguage, new List<string> { tariffDetailInfo.GeneralLedgerID });
                }
                if (!rateDA.CheckRateIDUnique(dataBase, tran, tariffDetailInfo.RateID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F9140_003", this.pubParasInfo.UserLanguage, new List<string> { tariffDetailInfo.RateID });
                }
                if (tariffDetailDA.InsertTariffDetail(dataBase, tran, tariffDetailInfo, whLoginID) > 0)
                {
                    strLineID = tariffDetailInfo.LineID.ToString();
                    new AuditDataBLL().InsertAuditData<object>(dataBase, tran, null, obj, AuditAction.Insert, this.pubParasInfo.UserID, "TARIFF_DETAIL", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strLineID;
        }

        public string UpdateDetail(object obj)
        {
            TariffDetailInfo tariffDetailInfo = obj as TariffDetailInfo;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strContractID = string.Empty;
            try
            {
                TariffDetailInfo oldTariffDetailInfo = tariffDetailDA.GetTariffDetailByID(dataBase, tran, tariffDetailInfo.TariffID, tariffDetailInfo.TariffType, this.dataBaseID[0], this.pubParasInfo);
                if (tariffDetailDA.UpdateTariffDetail(dataBase, tran, tariffDetailInfo, whLoginID) > 0)
                {
                    strContractID = tariffDetailInfo.TariffType;
                    new AuditDataBLL().InsertAuditData<object>(dataBase, tran, oldTariffDetailInfo, obj, AuditAction.Update, this.pubParasInfo.UserID, "TARIFF", this.dataBaseID[0]);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strContractID;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

